package fyf.com.carddesign.Bll;

import android.content.Context;
import android.util.Log;

import java.sql.Time;
import java.util.List;


import fyf.com.carddesign.Dao.Card;
import fyf.com.carddesign.Dao.CardDao;

import fyf.com.carddesign.Dao.DaoSession;
import fyf.com.carddesign.MyUtl.MyApplication;
import fyf.com.carddesign.MyUtl.MyUtl;
import fyf.com.carddesign.MyUtl.Tuple.Tuple;

/**
 * Card 业务逻辑类
 * Created by yxz on 2017/10/25.
 */

public class CardBll {
    private Context _context;
    private static CardBll _obj = null;
    private DaoSession _daoSession;

    /**
     * 构造函数
     *
     * @param _context 上下文信息
     */
    public CardBll(Context _context) {
        this._context = _context;
        _daoSession = MyApplication.getDaoInstant();
    }

    /**
     * 静态方法，直接调用
     *
     * @param context 上下文信息
     * @return CardBll对象
     */
    public static CardBll getInstance(Context context) {
        if (_obj == null)
            _obj = new CardBll(context);
        return _obj;
    }

    /**
     * 查询卡片总数据
     *
     * @return
     */
    public Tuple<String, List<Card>> selectAllCard() {
        try {
            CardDao cardDao = _daoSession.getCardDao();
            List<Card> list = cardDao.queryBuilder()
                    .build()
                    .list();
            return new Tuple<>("", list);
        } catch (Exception ex) {
            Log.d("yang", ex.getMessage());
            return new Tuple<>(ex.getMessage(), null);
        }
    }

    /**
     * 管理员添加卡片
     *
     * @param cardName 卡名称
     * @param notice   卡提示信息
     * @return Tuple<>二元组，1：是否成功标志；2，添加结果文字提示
     */
    public Tuple<Boolean, String> adminAddCard(String cardName, String notice, int num) {
        try {
            CardDao cardDao = _daoSession.getCardDao();

            for (int i = 0; i < num; i++) {
                Card card = new Card();
                card.setName(cardName + "   " + i);//卡名
                card.setText(notice);//卡编辑信息
                card.setAdd_date(MyUtl.getCurrentDay());//添加时间
                card.setIs_over_date(false);
                card.setIs_use(false);//是否使用了卡片（此处填写没有使用）
                cardDao.insert(card);
            }
            return new Tuple<>(true, "添加成功");
        } catch (Exception ex) {
            return new Tuple<>(false, "添加失败，原因：" + ex.getMessage());//添加失败
        }
    }

    /**
     * 删除指定的卡片（根据id删除）
     *
     * @param id 卡片的id
     * @return
     */
    public Tuple<Boolean, String> adminDeleteCard(Long id) {
        try {
            CardDao cardDao = _daoSession.getCardDao();
            cardDao.deleteByKey(id);
            return new Tuple<>(true, "删除成功");
        } catch (Exception ex) {
            return new Tuple<>(false, ex.getMessage());
        }
    }


//    public Tuple<Boolean, String> adminAlertCard(String name, String text, boolean isUse) {
//        try {
//            CardDao cardDao = _daoSession.getCardDao();
//
//
//        } catch (Exception ex) {
//            return new Tuple<>(false, ex.getMessage());
//        }
//
//
//    }

}
